from django.shortcuts import HttpResponse
import cv2
from modelscope.outputs import OutputKeys
from modelscope.pipelines import pipeline
from modelscope.utils.constant import Tasks

# Create your views here.
def carton(request):
    image_url = request.GET.get("image_url")
    img_cartoon = pipeline(Tasks.image_portrait_stylization, 
                       model='damo/cv_unet_person-image-cartoon_compound-models')
    result = img_cartoon(image_url)
    cv2.imwrite('static/images/result-cartoon.png', result[OutputKeys.OUTPUT_IMG])
    return HttpResponse("<img src={} >".format('/static/images/result-cartoon.png'))

def handPaintedCarton(request):
    image_url = request.GET.get("image_url")
    img_cartoon = pipeline(Tasks.image_portrait_stylization, model='damo/cv_unet_person-image-cartoon-handdrawn_compound-models')
    result = img_cartoon(image_url)
    cv2.imwrite('static/images/result-hand-carton.png', result[OutputKeys.OUTPUT_IMG])
    return HttpResponse("<img src={} >".format('/static/images/result-hand-carton.png'))

def sketchCarton(request):
    image_url = request.GET.get("image_url")
    img_cartoon = pipeline(Tasks.image_portrait_stylization, model='damo/cv_unet_person-image-cartoon-sketch_compound-models')
    result = img_cartoon(image_url)
    cv2.imwrite('static/images/result-sketch-carton.png', result[OutputKeys.OUTPUT_IMG])
    return HttpResponse("<img src={} >".format('/static/images/result-sketch-carton.png'))

def skinBeautification(request):
    image_url = request.GET.get("image_url")
    skin_retouching = pipeline(Tasks.skin_retouching,model='damo/cv_unet_skin-retouching')
    result = skin_retouching(image_url)
    cv2.imwrite('static/images/sb_reuslt.jpg', result[OutputKeys.OUTPUT_IMG])
    return HttpResponse("<img src={} >".format('/static/images/sb_reuslt.jpg'))

def ocrRecognition(request):
    image_url = request.GET.get("image_url")
    ocr_recognition = pipeline(Tasks.ocr_recognition, model='damo/cv_convnextTiny_ocr-recognition-licenseplate_damo', model_revision='v0.1')
    result = ocr_recognition(image_url)
    return HttpResponse(result['text'])

def segLink(request):
    image_url = request.GET.get("image_url")
    ocr_detection = pipeline(Tasks.ocr_detection, model='damo/cv_resnet18_ocr-detection-line-level_damo')
    result = ocr_detection(image_url)
    print(result, "-------------------\n\n")
    return HttpResponse(result)

